package com.tjc.common.pool;

import java.time.Duration;

/**
 * @Author: tjc
 * @description
 * @Date Created in 22:53 2022-08-06
 */
public class GenericObjectPoolConfig<T> {
    public static final int DEFAULT_MAX_VALUE = 8;
    public static final int DEFAULT_MIN_VALUE = 0;
    public static final boolean DEFAULT_BLOCK_WHEN_EXHAUSTED = true;
    private static final long DEFAULT_MAX_WAIT_MILLIS = -1L;
    public static final Duration DEFAULT_MAX_WAIT = Duration.ofMillis(DEFAULT_MAX_WAIT_MILLIS);

    private int maxTotal;
    private int maxIdle;
    private int minIdle;

    private boolean testOnCreate;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean testWhileIdle;

    private boolean lifo;
    private boolean fairness;

    public GenericObjectPoolConfig(Builder builder) {
        this.maxTotal = builder.getMaxTotal();
        this.maxIdle = builder.getMaxIdle();
        this.minIdle = builder.getMinIdle();
        this.testOnCreate = builder.getTestOnCreate();
        this.testOnBorrow = builder.getTestOnBorrow();
        this.testOnReturn = builder.getTestOnReturn();
        this.testWhileIdle = builder.getTestWhileIdle();
        this.lifo = builder.getLifo();
        this.fairness = builder.getFairness();
    }


    public int getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }

    public int getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public boolean getTestOnCreate() {
        return testOnCreate;
    }

    public void setTestOnCreate(boolean testOnCreate) {
        this.testOnCreate = testOnCreate;
    }

    public boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }

    public boolean getLifo() {
        return lifo;
    }

    public void setLifo(boolean lifo) {
        this.lifo = lifo;
    }

    public boolean getFairness() {
        return fairness;
    }

    public void setFairness(boolean fairness) {
        this.fairness = fairness;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private int maxTotal;
        private int maxIdle;
        private int minIdle;

        private boolean testOnCreate;
        private boolean testOnBorrow;
        private boolean testOnReturn;
        private boolean testWhileIdle;

        private boolean lifo;
        private boolean fairness;

        public GenericObjectPoolConfig build() {
            return new GenericObjectPoolConfig(this);
        }

        public int getMaxTotal() {
            return maxTotal;
        }

        public Builder setMaxTotal(int maxTotal) {
            this.maxTotal = maxTotal;
            return this;
        }

        public int getMaxIdle() {
            return maxIdle;
        }

        public Builder setMaxIdle(int maxIdle) {
            this.maxIdle = maxIdle;
            return this;
        }

        public int getMinIdle() {
            return minIdle;
        }

        public Builder setMinIdle(int minIdle) {
            this.minIdle = minIdle;
            return this;
        }

        public boolean getTestOnCreate() {
            return testOnCreate;
        }

        public Builder setTestOnCreate(boolean testOnCreate) {
            this.testOnCreate = testOnCreate;
            return this;
        }

        public boolean getTestOnBorrow() {
            return testOnBorrow;
        }

        public Builder setTestOnBorrow(boolean testOnBorrow) {
            this.testOnBorrow = testOnBorrow;
            return this;
        }

        public boolean getTestOnReturn() {
            return testOnReturn;
        }

        public Builder setTestOnReturn(boolean testOnReturn) {
            this.testOnReturn = testOnReturn;
            return this;
        }

        public boolean getTestWhileIdle() {
            return testWhileIdle;
        }

        public Builder setTestWhileIdle(boolean testWhileIdle) {
            this.testWhileIdle = testWhileIdle;
            return this;
        }

        public boolean getLifo() {
            return lifo;
        }

        public Builder setLifo(boolean lifo) {
            this.lifo = lifo;
            return this;
        }

        public boolean getFairness() {
            return fairness;
        }

        public Builder setFairness(boolean fairness) {
            this.fairness = fairness;
            return this;
        }
    }

}
